'use strict';
/**
 * Created by FreezeSoul on 2015/9/28.
 */
define([
        'angular',
        'adConfig',
        'app/modules/roles/define'
    ],
    function (angular, adConfig) {
        angular.module('ad.roles')
            .factory("adRolesService", ['$http', '$q', '_', 'adRequest', function ($http, $q, _, adRequest) {

                function getPage(current, size, search) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'user/role/search';
                    adRequest(url, {
                        criteria: search, pageIndex: current, pageSize: size
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function addItem(row) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'user/role/add';
                    adRequest(url, {
                        roleName: row.role_name,
                        roleType: row.role_type
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function deleteItems(rows) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'user/role/delete';
                    var roleIds = _.map(rows, function (row) {
                        return row.role_id;
                    });
                    adRequest(url, {
                        roleIds: angular.toJson(roleIds)
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function updateItem(row) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'user/role/update';
                    adRequest(url, {
                        roleId: row.role_id,
                        roleName: row.role_name,
                        roleType: row.role_type
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                function saveResources(roleId, resourceType, resourceIds) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'user/permissions/saveRoleResources';
                    adRequest(url, {
                        roleId: roleId,
                        resourceType: resourceType,
                        resourceIds: angular.toJson(resourceIds)
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }

                function queryResources(roleId, resourceType) {
                    var deferred = $q.defer();
                    var url = adConfig.baseServiceUrl + 'user/permissions/queryRoleResources';
                    adRequest(url, {
                        roleId: roleId,
                        resourceType: resourceType
                    }).success(function (data, status, headers, config) {
                        deferred.resolve(data);
                    }).error(function (data, status, headers, config) {
                        deferred.reject(status);
                    });
                    return deferred.promise;
                }


                return {
                    getPage: getPage,
                    addItem: addItem,
                    deleteItems: deleteItems,
                    updateItem: updateItem,
                    saveResources: saveResources,
                    queryResources: queryResources
                };
            }]);
    });